package com.gxa.modules.shiro;


import com.gxa.common.constant.Constants;
import com.gxa.modules.mapper.MenuMapper;
import com.gxa.modules.mapper.RoleMapper;
import com.gxa.modules.mapper.UserMapper;
import com.gxa.modules.pojo.Menu;
import com.gxa.modules.pojo.Role;
import com.gxa.modules.pojo.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class OAuth2Realm extends AuthorizingRealm {

    @Autowired(required = false)
    UserMapper userMapper;

    @Autowired(required = false)
    RoleMapper roleMapper;

    @Autowired(required = false)
    MenuMapper menuMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("----------权限-------------");
        String account =(String) principalCollection.getPrimaryPrincipal();
        List<Role> roles=roleMapper.queryRoleByAccount(account);
        if(!roles.isEmpty()){
            Set<String> roleTitle=new HashSet<>();
            List<Long> roleIds=new ArrayList<>();
//        角色名称去重
            for (Role role : roles) {
                roleTitle.add(role.getR_title());
                System.out.println("role:"+role.getR_title());
                roleIds.add(role.getR_id());
            }
            List<Menu> menus= menuMapper.queryMenuByRoleId(roleIds);
            Set<String> permissionName=new HashSet<>();
//        权限名称去重
            for (Menu menu : menus) {
                System.out.println("permission:"+menu.getM_url());
                permissionName.add(menu.getM_url());
            }
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
            info.setRoles(roleTitle);
            info.setStringPermissions(permissionName);
            return info;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("--------------------------------");
        String userAccount=(String)authenticationToken.getPrincipal();
        User user=userMapper.queryUserByAccount(userAccount);
        System.out.println(user);
        if(user==null||(user.getUser_enable()==Constants.USER_DEL_STATUS)){
            throw new UnknownAccountException("账号不存在");
        }
        if(user.getUser_enable()== Constants.USER_DISABLE_STATUS){
            throw new LockedAccountException("账号被锁");
        }
        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(user.getUser_account(),user.getUser_password(),"OAuth2Realm");
        info.setCredentialsSalt(ByteSource.Util.bytes(user.getUser_salt()));
        return info;
    }
}
